package com.weaver.seconddev.custom.qc1853114.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.cache.base.BaseCache;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.custom.qc1853114.service.ContractService;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.Util;
import com.weaver.workflow.common.framework.impl.WeaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ContractServiceImpl extends WeaService implements ContractService {

    @Autowired
    private static BaseCache cache;

    @Override
    public Map<String, Object> getDetailDocField(String workflowid, String browserType, String dbFieldName) {
        log.error("ContractServiceImpl---getDetailDocField---workflowid==" + workflowid + "---browserType==" + browserType + "---DBFieldName==" + dbFieldName);
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> apiMap = new HashMap<>();
        List<String> fieldList = new ArrayList<String>();
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
        RecordSet rs2 = I18nContextUtil.getBean(RecordSet.class);

        String sql = "select bf_.* from form_field bf_\n" +
                "        LEFT JOIN wfp_relateform  wfb_ on(wfb_.relatekey = bf_.form_id)\n" +
                "        where wfb_.workflowid  = '" + workflowid + "' and bf_.COMPONENT_KEY = 'RelateBrowser'\n" +
                "        and bf_.DATA_KEY ='" + browserType + "'";
        log.error("ContractServiceImpl---getDetailDocField---sql==" + sql);

        rs.executeForWf(sql);
        if (rs.next()) {
            String formId = rs.getString("form_id");
            String fieldId = rs.getString("id");
            log.error("ContractServiceImpl---getDetailDocField---formId==" + formId + "---fieldid==" + fieldId);
            // 查询明细表名称
            String sql1 = "SELECT table_name FROM form_table\n" +
                    "        WHERE form_id IN (SELECT id FROM sub_form WHERE form_id = '" + formId + "')";
            log.error("ContractServiceImpl---getDetailDocField---sql1==" + sql1);
            rs1.executeForWf(sql1);
            if (rs1.next()) {

//                String detailtable = "formtable_main_65_dt1";
                String detailtable = rs1.getString("table_name");
                log.error("ContractServiceImpl---getDetailDocField---detailtable==" + detailtable);

//                String id = "1166856517431394305";
                String id = Util.null2String(getDetailField(formId, dbFieldName));
                if (id.equals("")) {
                    returnMap.put("success", false);
                    return returnMap;
                }
                String idx = "";
                idx = detailtable.substring(detailtable.lastIndexOf('_') + 1);
                fieldList.add(id + "_" + idx);
                apiMap.put("fieldList", fieldList);

                String sql2 = "select fdlsm.* from fd_linkage_source_mapping fdlsm\n" +
                        "   LEFT JOIN fd_linkage_source fdls on fdlsm.ENTITY_ID = fdls.id\n" +
                        "   LEFT JOIN fd_linkage fdl on fdls.ENTITY_ID = fdl.id\n" +
                        "   where fdlsm.SOURCE_FIELD = '" + dbFieldName + "' and fdl.ref_id = '" + workflowid + "'" +
                        "   and fdlsm.DELETE_TYPE = '0' and fdls.DELETE_TYPE = '0'";
                log.error("ContractServiceImpl---getDetailDocField---sql2==" + sql2);
                rs2.executeForWf(sql2);

                if (rs2.next()) {
                    String targetField = rs2.getString("TARGET_FIELD");
                    //源附件字段id
//                    returnMap.put("fieldid","field1166943808472547328");
                    returnMap.put("fieldid", "field" + targetField);
                    returnMap.put("success", true);
                    //附件上传字段id
                    returnMap.put("uploadfield", apiMap.get("fieldList"));
                }

            }
        }
        log.error("ContractServiceImpl---getDetailDocField---returnMap==" + JSONObject.toJSONString(returnMap));
        return returnMap;
    }

    public String getDetailField(String formId, String fieldName) {
        String sql = "SELECT  f.ID FROM form_field f\n" +
                "JOIN sub_form sf ON f.sub_form_id = sf.id\n" +
                "WHERE sf.form_id = '" + formId + "' and f.data_key = '" + fieldName + "'";
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        rs.executeForWf(sql);
        if (rs.next() && !Util.null2String(rs.getString("ID")).equals("")) {
            return rs.getString("ID");
        }
        return null;
    }

}


